package com.example.highclasscomponent;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.highclasscomponent.adapter.PlanetBaseAdapter;
import com.example.highclasscomponent.pojo.Planet;

import java.util.List;

public class ListViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private List<Planet> mPlanetList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        ListView listView = findViewById(R.id.lv_plant);
        mPlanetList = Planet.getDefaultList();
        //构建适配器
        PlanetBaseAdapter adapter=new PlanetBaseAdapter(this, mPlanetList);
        listView.setAdapter(adapter);
        listView.setSelection(0);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "点击了"+mPlanetList.get(position).name, Toast.LENGTH_SHORT).show();
    }
}
